package com.huanghuai.retail.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huanghuai.retail.common.R;
import com.huanghuai.retail.dto.EmployeeDto;
import com.huanghuai.retail.entity.Employee;
import com.huanghuai.retail.service.EmployeeServer;
import com.huanghuai.retail.utils.RetailConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/employee")
@Slf4j
public class EmployeeController {

    @Autowired
    private EmployeeServer employeeServer;

    /**
     * 获取所有员工信息，并封装道dto中
     * @return
     */
    @GetMapping("/getPage")
    public R<Page> getAll(@RequestParam(defaultValue = "1")int page,
                          @RequestParam(defaultValue = "10")int pageSize,
                          String name, Integer categoryId){

        Page<EmployeeDto> dtoPage =
                employeeServer.getEmployeesDtoPage(page,pageSize,name,categoryId);

        if (dtoPage == null)
            return R.error("查询员工信息失败");

        return R.success(dtoPage);
    }

    @GetMapping("/getTypeList/{typeId}")
    public R<List<EmployeeDto>> getEmployeeDtoList(@PathVariable Integer typeId){
        return employeeServer.getEmployeeDtoList(typeId);
    }


    @PostMapping("/add")
    public R<EmployeeDto> addEmployee(@RequestBody Employee employee){
        EmployeeDto employeeDto = employeeServer.addEmployee(employee);

        if (employeeDto == null)
            R.error("用户信息出现问题，添加失败");

        return R.success(employeeDto);
    }

    @DeleteMapping("/del")
    public R<String> delById(Long id){

        if (id == null || id < 0)
            return R.error("删除失败");
        Employee employee = employeeServer.getById(id);
        if (employee == null)
                return R.error("删除失败");
        employee.setStatus(0);

        employeeServer.updateById(employee);

        return R.success("禁用成功");
    }

    @PutMapping("/updat")
    public R<String> update(@RequestBody Employee employee){
        //判断信息是否全
        if (employee.getCategoryId() == null || employee.getSex() == null ||
            employee.getPhone() == null || employee.getName() == null ||
            employee.getUserName() == null || employee.getPassword() == null
            )
            return R.error("修改失败");

        employeeServer.updateById(employee);
        return R.success("修改成功");
    }


}
